size <- 12. Later this will be the number of rows of the matrix.x <- rnorm( size ).x1 by adding (on average 10 times smaller) noise to x: x1 <- x + rnorm( size )/10.x and x1 should be close to 1.0: check this with function cor.x2 and x3 by adding (other) noise to x.size <- 12
x <- rnorm( size )
x1 <- x + rnorm( size )/10
cor( x, x1 )
[1] 0.9864459
x2 <- x + rnorm( size )/10
x3 <- x + rnorm( size )/10
x1, x2 and x3 column-wise into a matrix using m <- cbind( x1, x2, x3 ).m.m.heatmap( m, Colv = NA, Rowv = NA, scale = "none" ).m <- cbind( x1, x2, x3 )
class( m )
[1] "matrix" "array"
head( m )
x1 x2 x3
[1,] -0.66324649 -0.69032841 -0.50152243
[2,] -0.40851106 -0.33090058 -0.35629681
[3,] -1.81746466 -2.05983543 -1.95535052
[4,] -0.86225896 -0.67796342 -0.83358241
[5,] 0.26450445 0.12371467 0.20483381
[6,] 0.05756759 -0.01379862 -0.01625961
heatmap( m, Colv = NA, Rowv = NA, scale = "none" ) # high is dark red, low is yellow
# x1, x2, x3 follow similar color pattern, they should be correlated
y1…y4 (but not correlated with x), of the same length size.m from columns x1…x3,y1…y4 in some random order.y <- rnorm( size )
y1 <- y + rnorm( size )/10
y2 <- y + rnorm( size )/10
y3 <- y + rnorm( size )/10
y4 <- y + rnorm( size )/10
m <- cbind( y4, y3, x2, y1, x1, x3, y2 )
heatmap( m, Colv = NA, Rowv = NA, scale = "none" ) # high is dark red, low is yellow
cc <- cor( m ) to build the matrix of correlation coefficients between columns of m.round( cc, 3 ) to show this matrix with 3 digits precision.cc <- cor( m )
round( cc, 3 ) #
y4 y3 x2 y1 x1 x3 y2
y4 1.000 0.982 -0.099 0.992 -0.076 -0.062 0.991
y3 0.982 1.000 0.014 0.989 0.011 0.035 0.980
x2 -0.099 0.014 1.000 -0.060 0.976 0.976 -0.101
y1 0.992 0.989 -0.060 1.000 -0.042 -0.024 0.992
x1 -0.076 0.011 0.976 -0.042 1.000 0.977 -0.086
x3 -0.062 0.035 0.976 -0.024 0.977 1.000 -0.081
y2 0.991 0.980 -0.101 0.992 -0.086 -0.081 1.000
heatmap( cc, symm = TRUE, scale = "none" )
# E.g. value for (row: x1, col: y1) is the corerlation of vectors x1, y1.
# Values of 1.0 are on the diagonal: e.g. x1 is perfectly correlated with x1.
# Correlations between x, x vectors are close to 1.0.
# Correlations between y, y vectors are close to 1.0.
# Correlations between x, y vectors are close to 0.0.
Copyright © 2022 Biomedical Data Sciences (BDS) | LUMC